package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.WorkOvertimeConfigDTO;
import com.eastfair.venueservice.dto.WorkOvertimeConfigPageQuery;
import com.eastfair.venueservice.dto.WorkOvertimeConfigSaveDTO;
import com.eastfair.venueservice.dto.WorkOvertimeConfigUpdateDTO;
import com.eastfair.venueservice.entity.WorkOvertimeConfig;
import com.eastfair.venueservice.service.WorkOvertimeConfigService;
import com.eastfair.venueservice.vo.WorkOvertimeConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 加班规则配置
 * </p>
 *
 * @author dq
 * @date 2022-10-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/workOvertimeConfig")
@Api(value = "WorkOvertimeConfig", tags = "加班规则配置")
@PreAuth(replace = "venueservice:workOvertimeConfig:")
public class WorkOvertimeConfigController extends SuperController<WorkOvertimeConfigService, Long, WorkOvertimeConfig, WorkOvertimeConfigPageQuery, WorkOvertimeConfigSaveDTO, WorkOvertimeConfigUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private WorkOvertimeConfigService workOvertimeConfigService;

    @Override
    public void handlerResult(IPage<WorkOvertimeConfig> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<WorkOvertimeConfig> workOvertimeConfigList = list
                .stream()
                .map((map) -> WorkOvertimeConfig.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(workOvertimeConfigList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<WorkOvertimeConfigPageQuery> params) {
        WorkOvertimeConfigPageQuery pageQuery = params.getModel();
    }

    /**
     * 展会ID查询加班规则配置VO.
     *
     * @param exhibitionManageId the exhibition manage id
     * @return the by exhibition manage id
     */
    @ApiOperation("展会ID查询加班规则配置VO")
    @GetMapping("/get/exhibition/{exhibitionManageId}")
    public R<WorkOvertimeConfigVO> getByExhibitionManageId(@PathVariable("exhibitionManageId") Long exhibitionManageId) {
        log.info("getByExhibitionManageId - 展会ID查询加班规则配置VO, exhibitionManageId={}", exhibitionManageId);
        try {
            return R.successDef(workOvertimeConfigService.getVOByExhibitionManageId(exhibitionManageId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 保存加班规则配置.
     *
     * @param workOvertimeConfigDTO the work overtime config dto
     * @return the r
     */
    @ApiOperation("保存加班规则配置")
    @SysLog("添加加班规则")
    @PostMapping("/save")
    public R<WorkOvertimeConfig> saveWorkOvertimeConfig(@RequestBody WorkOvertimeConfigDTO workOvertimeConfigDTO) {
        log.info("saveWorkOvertimeConfig - 保存加班规则配置, workOvertimeConfigDTO={}", workOvertimeConfigDTO);
        return R.successDef(workOvertimeConfigService.saveWorkOvertimeConfig(workOvertimeConfigDTO));
    }

    /**
     * 更新加班规则配置.
     *
     * @param workOvertimeConfigDTO the work overtime config dto
     * @return the r
     */
    @ApiOperation("更新加班规则配置")
    @SysLog("修改加班规则")
    @PostMapping("/update")
    public R<WorkOvertimeConfig> updateWorkOvertimeConfig(@RequestBody WorkOvertimeConfigDTO workOvertimeConfigDTO) {
        log.info("updateWorkOvertimeConfig - 更新加班规则配置, workOvertimeConfigDTO={}", workOvertimeConfigDTO);
        return R.successDef(workOvertimeConfigService.updateWorkOvertimeConfig(workOvertimeConfigDTO));
    }
}
